clear all 
set more off 
set maxvar 15000 
clear matrix

estimates clear

* 1. IGE (1910/1940 DIFFERENCE)

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    keep if decade==1910 | decade==1940     
        
    gen cohort_late = decade==1940
    gen interaction = cohort_late * log_father_baseline

    reg log_son_baseline log_father_baseline cohort_late interaction  [aw=wgt_sex_race], robust
    estimates store ige_diff 

* 2. IGE (LINEAR DIFFERENCE)
        
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if decade>=1910 & decade<=1940 
    keep if baseline_sample==1

    * Generate interaction term for decline
    sort dob
    egen time = group(dob)
    replace time = time-1   
    
    gen interaction = log_father_baseline*time
        
    reg log_son_baseline log_father_baseline interaction i.time  [aw=wgt_sex_race], robust
            estimates store ige_linear 
            
* 3. RANK (1910/1940 DIFFERENCE) 
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    keep if decade==1910 | decade==1940       
    
    gen cohort_late = decade==1940
    gen interaction = cohort_late * rank_father_baseline
    
    reg rank_son_baseline rank_father_baseline cohort_late interaction  [aw=wgt_sex_race], robust
    estimates store rank_diff 
            
* 4. RANK (LINEAR DIFFERENCE)
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if decade>=1910 & decade<=1940 
    keep if baseline_sample==1

    * Generate interaction term for decline
    sort dob
    egen time = group(dob)
    replace time = time-1   
        
    gen interaction = rank_father_baseline*time
    
    reg rank_son_baseline rank_father_baseline interaction i.time  [aw=wgt_sex_race], robust
        estimates store rank_linear 

    label var interaction "Difference"

    * Figure
    #delimit ; 
    esttab ige_diff ige_linear rank_diff rank_linear using "$Mydirectory2/appendix_a/table_main_estimates_diff.tex", replace 
    booktabs nostar label  keep(interaction) nobase noomit nonote alignment(c c) width(1.2\textwidth) se(4) b(4) brackets
    mtitles("1910--1940 difference" "Linear difference" "1910--1940 difference" "Linear difference" ) 
    mgroups("IGE" "Rank-rank", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
    stats(N, fmt(%9.2gc) label("Observations"));
    #delimit cr
    
